package demo.icanci.reflect;

import java.lang.reflect.Constructor;

/**
 * @author icanci
 * @date Created in 2021/02/04 23:48
 * @version 1.0
 */
public class ConstructorController {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("demo.icanci.reflect.ReflectTarget");
        Constructor<?>[] constructors = clazz.getConstructors();
        System.out.println("获取所有的共有构造方法");
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println("获取所有的构造方法");
        Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        System.out.println("获取单个参数的构造方法");
        Constructor<?> constructor = clazz.getConstructor(char.class, int.class);
        System.out.println(constructor);
        Object c = constructor.newInstance('c', 12);
        System.out.println(c);
        System.out.println("获取私有的构造方法");
        Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(int.class);
        System.out.println(declaredConstructor);
        // 忽略访问修饰符
        declaredConstructor.setAccessible(true);
        Object o = declaredConstructor.newInstance(19);
        System.out.println(o);
    }
}
